package com.maigao.core.exception;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
public class ServerException extends BaseException {

    private Integer code;

    public ServerException(ServerCode serverCode) {
        super(serverCode.getMessage());
        this.code = serverCode.getCode();
    }

    public ServerException(Integer code, String message) {
        super(message);
        this.code = code;
    }

    @Override
    public String toString() {
        String s = getClass().getName();
        String message = getLocalizedMessage();
        return (message != null) ? (s + ": " + code + " " + message) : s;
    }

    public static ServerException code(int code, String message, Object... args) {
        if (args.length == 0) {
            return new ServerException(code, message);
        }
        return new ServerException(code, String.format(message, args));
    }

    public static ServerException code(ServerCode code, Object... args) {
        return code(code.getCode(), code.getMessage(), args);
    }

}